﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using Microsoft.Xna.Framework.Graphics;
using MdxLib.Model;
using System.IO;
using UnityEngine.IO;
using System;
using UnityEngine.Graphics.Texture;
using System.Diagnostics;
using UnityEngine.Gameplay;
using UnityEngine.Gameplay.Match;

namespace UnityEngine.Graphics
{
    public class TextureRenderer
    {
        public CTexture CTexture { get; set; }
        public Texture2D Texture { get; set; }

        public TextureRenderer(CTexture texture, GraphicsDevice device)
        {
            this.CTexture = texture;

            if (this.CTexture.FileName == null || this.CTexture.FileName.Length == 0 ||
                this.CTexture.FileName.Contains("TeamColor") == true ||
                this.CTexture.FileName.Contains("TeamGlow") == true)
            {
                ReplaceReplaceableTexture(null);
            }
            else
            {
                this.Texture = TextureManager.Instance.GetTexture(this.CTexture.FileName);
            }
        }

        public void ReplaceReplaceableTexture(TeamRelationType teamType)
        {
            int team = (int)teamType;
            Debug.Assert(team >= 0 && team < (int)TeamPositionType.Count);

            if (this.CTexture.FileName == null || this.CTexture.FileName.Length == 0)
            {
                {
                    if (this.CTexture.FileName.Contains("TeamColor") == true)
                    {
                        this.Texture = TextureManager.Instance.TeamColor[team];
                        return;
                    }

                    if (this.CTexture.FileName.Contains("TeamGlow") == true)
                    {
                        this.Texture = TextureManager.Instance.TeamGlow[team];
                        return;
                    }

                    switch (this.CTexture.ReplaceableId)
                    {
                        case 11:
                            this.Texture = TextureManager.Instance.Cliff1;
                            break;
                        case 2:
                            this.Texture = TextureManager.Instance.TeamGlow[team];
                            break;
                        case 1:
                            this.Texture = TextureManager.Instance.TeamColor[team];
                            break;
                        case 32:
                            this.Texture = TextureManager.Instance.TeamColor[team];
                            break;
                        default:
                            this.Texture = TextureManager.Instance.TeamGlow[team];
                            break;
                    }
                }
            }
        }

        public void ReplaceReplaceableTexture(Texture2D texture)
        {
            if (this.CTexture.FileName == null || this.CTexture.FileName.Length == 0)
            {
                if (texture != null)
                    this.Texture = texture;
                else
                {
                    if (this.CTexture.FileName.Contains("TeamColor") == true)
                    {
                        this.Texture = TextureManager.Instance.TeamColor[(int)TeamRelationType.Self];
                        return;
                    }

                    if (this.CTexture.FileName.Contains("TeamGlow") == true)
                    {
                        this.Texture = TextureManager.Instance.TeamGlow[(int)TeamRelationType.Self];
                        return;
                    }

                    switch (this.CTexture.ReplaceableId)
                    {
                        case 11:
                            this.Texture = TextureManager.Instance.Cliff1;
                            break;
                        case 2:
                            this.Texture = TextureManager.Instance.TeamGlow[(int)TeamRelationType.Self];
                            break;
                        case 1:
                            this.Texture = TextureManager.Instance.TeamColor[(int)TeamRelationType.Self];
                            break;
                        case 32:
                            this.Texture = TextureManager.Instance.TeamColor[(int)TeamRelationType.Self];
                            break;
                        default:
                            this.Texture = TextureManager.Instance.TeamColor[(int)TeamRelationType.Self];
                            break;
                    }
                }
            }
        }
    }
}
